package com.springapp.utils;

import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

/**
 * Created by Administrator on 16-9-13.
 *
 * 这个不行，每次都是获得新的
 */
public class MongoDbUtil2 {

    private MongoClient mc = null;


    /**
     *
     * @param host  localhost
     * @param port  27017
     */
    public MongoDbUtil2(String host, int port){
        mc = new MongoClient(host,port);
    }

    public MongoClient getMongoClient(){
        return mc;
    }

    public void closeMongoClient(){
        mc.close();
    }

    // 打印查询的结果集
    public void printResult(String doing, FindIterable<Document> iterable) {
        System.out.println(doing);
        iterable.forEach(new Block<Document>() {
            public void apply(final Document document) {
                System.out.println(document);
            }
        });
        System.out.println("------------------------------------------------------");
        System.out.println();
    }


}
